package edu.mit.media.fv.aqwidgets
{
	import edu.mit.media.fv.blocks.*;
	import edu.mit.media.fv.components.*;
	import edu.mit.media.fv.events.*;
	import edu.mit.media.fv.fluidspace.*;
	import edu.mit.media.fv.icons.*;
	import edu.mit.media.fv.layers.*;
	
	import flash.display.*;
	import flash.events.*;
	import flash.filters.*;
	import flash.text.*;
	import flash.utils.Timer;
	
	public class AQActiveTray extends AQWidget
	{
		private static var DEFAULT_WIDTH:int = 600;
		private static var DEFAULT_HEIGHT:int = 70;
		
		private var viewport:Sprite;
		private var selectivity:DisplayObject;
		private var layer:ActiveLayer;
		private var slider:FVHorizontalSlider;
		private var isBroadcast:Boolean = false;
		private var timer:Timer;
		
		
		public function AQActiveTray()
		{
			super();
			
			this.timer = new Timer(50, 5);
			
			viewport = new Sprite();
			this.addChild(this.viewport);
			
			var mask:Sprite = new Sprite();
			mask.graphics.beginFill(0xFFFFFF, 1.0);
			mask.graphics.drawRect(0,0,DEFAULT_WIDTH+1,DEFAULT_HEIGHT+1);
			mask.graphics.endFill();
			this.addChild(mask);
			this.mask = mask;
			
			this.layer = new ActiveLayer(0,0,DEFAULT_WIDTH-DEFAULT_HEIGHT,DEFAULT_HEIGHT);
			this.viewport.addChild(this.layer);
			this.slider = new FVHorizontalSlider(10+DEFAULT_WIDTH,0,DEFAULT_WIDTH-20-DEFAULT_HEIGHT,DEFAULT_HEIGHT);
			this.viewport.addChild(this.slider);
			
			this.selectivity = new AQBroadcastIcon();
			this.selectivity.x = DEFAULT_WIDTH-40;
			this.selectivity.y = 0;
			this.addChild(this.selectivity);
			
			this.selectivity.addEventListener(MouseEvent.CLICK, changeSelectivity);
			this.timer.addEventListener(TimerEvent.TIMER, processTimerOn);
			this.timer.addEventListener(TimerEvent.TIMER_COMPLETE, processTimerComplete);
			
		}
		public override function getBlockContainers():Array{
			if(this.isBroadcast){
				return [];
			}else{
				return [this.layer];
			}			
		}
		public function getBroadcastRecipients():Array{
			return this.layer.blockIDs;
		}
		public function getBroadcastStrength():Number{
			return this.slider.value;
		}
		
		private function processTimerOn(event:TimerEvent):void{
			if(this.isBroadcast){
				this.viewport.x = -1*timer.currentCount/timer.repeatCount*DEFAULT_WIDTH;
			}else{
				this.viewport.x = -1*(timer.repeatCount-timer.currentCount)/timer.repeatCount*DEFAULT_WIDTH;
			}
			
		}
		private function processTimerComplete(event:TimerEvent):void{
			if(this.isBroadcast){
				this.viewport.x = -DEFAULT_WIDTH;
				timer.reset();
			}else{
				this.viewport.x = 0;

				timer.reset();
			}
			lock = false;
			
		}
		private var lock:Boolean = false;
		private function changeSelectivity(event:Event):void{
			if(!this.lock){
				this.isBroadcast = !this.isBroadcast;
				if(this.isBroadcast){
					this.isBroadcast = true;
				}else{
					this.isBroadcast = false;
				}
				timer.start();
				lock = true;
			}
		}		
	}
}